package org.glandais.profiler.beans.xstream;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.glandais.profiler.Agent;

public class Config implements ConfigMBean {

	public static ConfigMBean loadConfig(File f) throws Exception {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
		ObjectInputStream xOIS = Agent.xstream.createObjectInputStream(bis);
		return (ConfigMBean) xOIS.readObject();
	}

	public static void saveConfig(File f) throws Exception {
		FileOutputStream fos = new FileOutputStream(f);
		ObjectOutputStream xOOS = Agent.xstream.createObjectOutputStream(fos);
		xOOS.writeObject(new Config());
		xOOS.close();
	}

	private List<Include> includes = new ArrayList<Include>();
	private List<String> excludes = new ArrayList<String>();
	private List<String> entryMethods = new ArrayList<String>();

	private int minLengthMsGlobal = 200;
	private int minLengthMsMethod = 10;

	private boolean saveSnapshots = false;
	private boolean dumpStatistics = false;
	private boolean logError = true;
	private boolean logVerbose = false;
	private boolean logTraceInstrumentation = false;
	private boolean logTraceMethodLogs = false;

	private String directory = "C:\\logs\\profiling\\";

	public Config() {
		super();

		Include inc = new Include();
		inc.setPackage("org.glandais");
		inc.getExcludes().add("org.glandais.not");
		includes.add(inc);
		inc = new Include();
		inc.setPackage("com.progress");
		includes.add(inc);

		excludes.add("sun.");
		excludes.add("java.");
		excludes.add("javax.");

		entryMethods.add(".");
	}

	public void logVerbose(String message) {
		if (isLogVerbose()) {
			System.out.println(message);
		}
	}

	public void logTraceInstrumentation(String message) {
		if (isLogTraceInstrumentation()) {
			System.out.println(message);
		}
	}

	public void logTraceMethodLogs(String message) {
		if (isLogTraceMethodLogs()) {
			System.out.println(message);
		}
	}

	public void logError(String message) {
		if (isLogError()) {
			System.err.println(message);
		}
	}

	public void logError(String message, Throwable t) {
		if (isLogError()) {
			System.err.println(message);
			t.printStackTrace(System.err);
		}
	}

	public List<Include> getIncludes() {
		return includes;
	}

	public void setIncludes(List<Include> includes) {
		this.includes = includes;
	}

	public List<String> getExcludes() {
		return excludes;
	}

	public void setExcludes(List<String> excluded) {
		this.excludes = excluded;
	}

	public List<String> getEntryMethods() {
		return entryMethods;
	}

	public void setEntryMethods(List<String> entryMethods) {
		this.entryMethods = entryMethods;
	}

	public int getMinLengthMsGlobal() {
		return minLengthMsGlobal;
	}

	public void setMinLengthMsGlobal(int minLengthMsGlobal) {
		this.minLengthMsGlobal = minLengthMsGlobal;
	}

	public int getMinLengthMsMethod() {
		return minLengthMsMethod;
	}

	public void setMinLengthMsMethod(int minLengthMsMethod) {
		this.minLengthMsMethod = minLengthMsMethod;
	}

	public boolean isSaveSnapshots() {
		return saveSnapshots;
	}

	public void setSaveSnapshots(boolean saveSnapshots) {
		this.saveSnapshots = saveSnapshots;
	}

	public boolean isLogError() {
		return logError;
	}

	public void setLogError(boolean logError) {
		this.logError = logError;
	}

	public boolean isLogVerbose() {
		return logVerbose;
	}

	public void setLogVerbose(boolean logVerbose) {
		this.logVerbose = logVerbose;
	}

	public boolean isLogTraceInstrumentation() {
		return logTraceInstrumentation;
	}

	public void setLogTraceInstrumentation(boolean logTraceInstrumentation) {
		this.logTraceInstrumentation = logTraceInstrumentation;
	}

	public boolean isLogTraceMethodLogs() {
		return logTraceMethodLogs;
	}

	public void setLogTraceMethodLogs(boolean logTraceMethodLogs) {
		this.logTraceMethodLogs = logTraceMethodLogs;
	}

	public String getDirectory() {
		return directory;
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	public boolean isDumpStatistics() {
		return dumpStatistics;
	}

	public void setDumpStatistics(boolean dumpStatistics) {
		this.dumpStatistics = dumpStatistics;
	}

}
